<?php

declare(strict_types=1);

namespace App\Api\Controller;

use App\Api\Middleware\ApiLockMiddleware;
use App\Api\Middleware\CheckAuthMiddleware;
use App\Api\Middleware\ResponseMiddleware;
use App\Api\Request\LesseeRequest;
use App\Api\Service\LesseeService;
use Hyperf\Contract\LengthAwarePaginatorInterface;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;

#[Controller(prefix: 'api'), Middlewares([ResponseMiddleware::class])]
class LesseeController
{
    /**
     * 承租人列表
     * @param LesseeService $service
     * @return LengthAwarePaginatorInterface
     */
    #[GetMapping('lessees'), Middlewares([CheckAuthMiddleware::class])]
    public function index(LesseeService $service)
    {
        return $service->index();
    }

    /**
     * 添加承租人
     * @param LesseeRequest $request
     * @param LesseeService $service
     * @return void
     */
    #[PostMapping('lessees'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function store(LesseeRequest $request, LesseeService $service): void
    {
        $service->store($request->validated());
    }

    /**
     * 修改承租人
     * @param mixed $id
     * @param LesseeRequest $request
     * @param LesseeService $service
     * @return void
     */
    #[PutMapping('lessees/{id}'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function update(mixed $id, LesseeRequest $request, LesseeService $service): void
    {
        $service->update($id, $request->validated());
    }

    /**
     * 更新承租人默认地址
     * @param mixed $id
     * @param LesseeRequest $request
     * @param LesseeService $service
     * @return void
     */
    #[PutMapping('lessees/{id}/default'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function updateDefault(mixed $id,LesseeRequest $request, LesseeService $service): void
    {
        // 参数验证
        $request->validated();
        $service->updateDefault($id);
    }

    /**
     * 删除承租人
     * @param mixed $id
     * @param LesseeService $service
     * @return void
     */
    #[DeleteMapping('lessees/{id}'), Middlewares([CheckAuthMiddleware::class, ApiLockMiddleware::class])]
    public function destroy(mixed $id, LesseeService $service): void
    {
        $service->destroy($id);
    }
}
